//
//  ReplayDetailViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/26.
//

#import "ReplayDetailViewController.h"
#import "DetailHeaderView.h"
#import "LeaingMessageTableViewCell.h"
#import "CommonUtil.h"
#import "ReplayViewController.h"

@interface ReplayDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) DetailHeaderView * headerView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSMutableArray * models;
@property (strong, nonatomic) UIButton *toolBar;
@end

@implementation ReplayDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"留言板详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self testData];
    [self.view addSubview:self.toolBar];
    
    MessageModel *model = [[MessageModel alloc] init];
    model.isDetail = YES;
    model.content = @"给大家参考一下，看到十几期了，不改料，对错勿怪，慎参！！！"; // 测试单行文字
    model.imageCount = 9;
    self.headerView.model = model;
    self.headerView.height = AD(20) + AD(60) + model.feedHeight + AD(3);
}

// 假数据
- (void)testData{
    self.models = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        MessageModel *model = [[MessageModel alloc] init];
        model.isDetail = YES;
        model.content = @"根据国家卫健委24日下午公布的最新数据"; // 测试单行文字
        if (i < 10) {
            model.imageCount = i; //测试图片展示
        }
        if (i == 1 || i == 5) {
            model.content = @"环球时报记者 倪浩】根据国家卫健委24日下午公布的最新数据，截至5月23日，全国累计报告接种新冠病毒疫苗51085.8万剂次。在安徽和辽宁新一轮局部疫情之后，中国疫苗接种明显提速。5月19日之前的8天时间"; //测试多行文字
        }else if (i == 2){
            [model addReayTest];
        }
        
        
        [self.models addObject:model];
    }
    [self.tableView reloadData];
}


- (void)toolBarBtnClick:(UIButton *)sender{
    [self presentViewController:[ReplayViewController new] animated:YES completion:nil];
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeaingMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.models.count > indexPath.row) {
        cell.model = self.models[indexPath.row];
    }
    cell.superVC = self;
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.navigationController pushViewController:[ReplayDetailViewController new] animated:YES];


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.models.count > indexPath.row) {
        MessageModel *model = self.models[indexPath.row];
        return model.cellHeight;
    }
    return 0;
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kNavBarAndStatusBarHeight)];
        [_tableView registerClass:[LeaingMessageTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, kTabBarHeight, 0);
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
    }
    return _tableView;
}

-(DetailHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[DetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        _headerView.superVC = self;
        _headerView.clipsToBounds = YES;
    }
    return _headerView;
}


-(UIButton *)toolBar{
    if (!_toolBar) {
        _toolBar = [UIButton buttonWithType:UIButtonTypeCustom];
        [_toolBar addTarget:self action:@selector(toolBarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _toolBar.backgroundColor = RGB(249.0, 249.0, 249.0);
        _toolBar.frame = CGRectMake(0, SCREEN_HEIGHT-AD(80), SCREEN_WIDTH, AD(80));
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(AD(15), AD(15), SCREEN_WIDTH-AD(30), AD(40))];
        v.backgroundColor = [UIColor whiteColor];
        v.layer.cornerRadius = AD(5);
        v.layer.borderColor = RGB(240.0, 240.0, 240.0).CGColor;
        v.layer.borderWidth = AD(1);
        v.userInteractionEnabled = false;
        [_toolBar addSubview:v];
        
        
    }
    return _toolBar;
}


@end
